package com.wan37.logic.handler.message;

import com.wan37.logic.backpack.weaponry.service.WeaponryService;
import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.UserWeaponryDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.UserWeaponryService;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.service.cache.WeaponryServiceCache;
import com.wan37.logic.utils.BuffUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : luoyong
 * @date : 2020-06-17 15:54
 **/
public class CGameWeaponryHandler extends MsgHandler {

    private final static Logger logger = LoggerFactory.getLogger(CGameWeaponryHandler.class);

    private RoleServiceCache roleServiceCache;
    private RouteUtils routeUtils;
    private UserServiceCache userServiceCache;
    private WeaponryService weaponryService;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameWeaponryHandler(MessageVO messageVO) {
        super(messageVO);
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.weaponryService = SpringContextHolder.getBean("weaponryService");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameBackpack.CGameWeaponryMsg weaponryMsg = (GameBackpack.CGameWeaponryMsg) messageVO.getMessage();
        UserServiceCache.User user = userServiceCache.getUserServiceMap().get(messageVO.getUserId());
        Map<String, WeaponryVO> weaponryVOMap = user.getWeaponryVOMap();
        int code = weaponryMsg.getCode();
        switch (code) {
            case CommonBackpack.ADD_WEAPONRY:

                break;
            case CommonBackpack.GET_WEAPONRY:
                getWeaponry(weaponryVOMap);
                break;
            case CommonBackpack.UPDATE_WEAPONRY_USAGE_USE:
                RoleServiceCache.Role role = roleServiceCache.getRoleServiceMap().get(weaponryMsg.getRoleId());
                weaponryVOMap.values().forEach(weaponryVO -> {
                    if (weaponryVO.getSelfWeaponryId().equals(weaponryMsg.getWeaponry().getSelfWeaponryId())) {
                        weaponryService.updateWeaponryUsageUse(weaponryVO, user, role, messageVO);
                    }
                });
                break;
            case CommonBackpack.UPDATE_WEAPONRY_USAGE_UNUSED:
                RoleServiceCache.Role role1 = roleServiceCache.getRoleServiceMap().get(weaponryMsg.getRoleId());
                weaponryVOMap.values().forEach(weaponryVO -> {
                    if (weaponryVO.getSelfWeaponryId().equals(weaponryMsg.getWeaponry().getSelfWeaponryId())) {
                        weaponryService.updateWeaponryUsageUnused(weaponryVO, user, role1, messageVO);
                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * 获取自身全部装备信息
     *
     * @param weaponryVOMap
     */
    public void getWeaponry(Map<String, WeaponryVO> weaponryVOMap) {
        routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS,
                messageVO.getUserId() + "装备信息获取成功！", weaponryVOMap, messageVO);
        logger.info("用户：" + messageVO.getUserId() + " 获取装备消息已返回！！");
    }


}
